/*********************************************************************
 * INCLUDES
 */
#include "OSAL.h"
#include "log.h"
#include "halPeripheral.h"
#include "i2c_master.h"


#define I2C_SLAVE_ADDR 0xA0
#define I2C_TARGET_ADD I2C_SLAVE_ADD

static uint8 iicMaster_TaskID;   // Task ID for internal task/event processing

/**
* @fn void sample_i2c_master_init()
* @brief Initialize i2c master function
* @param NONE
* @return NONE.
*/
void sample_i2c_master_init()
{	
	I2cCfg_t cfg;
	cfg.i2cScl = P14;
	cfg.i2cSda = P23;
	cfg.i2cRole = I2C_ROLE_MASTER;
	cfg.i2cClock = I2C_CLOCK_400K;
	cfg.i2cAddressBit = I2C_ADDRESS_BIT_7;
	cfg.i2cAddress = I2C_SLAVE_ADDR;
	cfg.i2cWorkMode = I2C_WORK_MODE_INT;
	cfg.evtHdlCB = NULL;
	HalI2cInit(AP_I2C0, &cfg);
	LOG("I2C master Test\r\n");
}

/**
* @fn void sample_i2c_master_test()
* @brief i2c master sample function
* @param NONE
* @return NONE.
*/
void sample_i2c_master_test()
{
    static uint8_t step = 0;
    uint8_t *txBuf = "ABCDEFGHIJKLMNOPQRSTUVWXYZ+-*@";
	uint8_t  rxBuffer[40] = {0};
	uint8_t i;
    //uint8_t rxBuf[16] = {0};
    switch(step&0x01){
        case 0:	//write EEPROM
			LOG("Write-");
            HalI2cMasterWrite(AP_I2C0, 0xA0, 0, 2, txBuf, osal_strlen((char*)txBuf));
			while(HalI2cGetTransferComplete(AP_I2C0) != ERR_NONE);
        break;
        case 1:	//read EEPROM
            //LOG("Read Start\r\n");
			LOG("Read:");
			HalI2cMasterRead(AP_I2C0, 0xA1, 0, 2, rxBuffer, 26);
			while(HalI2cGetTransferComplete(AP_I2C0) != ERR_NONE);
			for(i=0;i<26;i++) {
				if(rxBuffer[i] != txBuf[i]) {
					break;
				}
			}
			if(i<26) {
				LOG("Fail:");
				for(i=0;i<26;i++) {
					LOG("%c",rxBuffer[i]);
				}
				LOG("\r\n");
				//reaad again
				HalI2cMasterRead(AP_I2C0, 0xA1, 0, 2, rxBuffer, 26);
				while(HalI2cGetTransferComplete(AP_I2C0) != ERR_NONE);
				for(i=0;i<26;i++) {
					if(rxBuffer[i] != txBuf[i]) {
						break;
					}
				}
				if(i<26) {
					LOG("ReadAgainFail:");
					for(i=0;i<26;i++) {
						LOG("%d",rxBuffer[i]);
					}
					LOG("\r\n");
				}
				else {
					LOG("ReadAgainOK\r\n");
				}
			}
			else {
				LOG("OK\r\n");
			}
        break;
    }
    step++;
}


/*********************************************************************
 * PUBLIC FUNCTIONS
 */

/*********************************************************************
 * @fn      SimpleBLEPeripheral_Init
 *
 * @brief   Initialization function for the Peripheral Test Task.
 *          It calls i2c master sample initialization function and sets a
 *          timer to call USR_TEST_TIMER1_EVT events.
 *
 * @param   task_id - the ID assigned by OSAL.  This ID will be
 *                    used to send messages and set timers.
 *
 * @return  none
 */
void HalPeripheral_Init(uint8 task_id)
{
	iicMaster_TaskID = task_id;
	sample_i2c_master_init();
	VOID osal_start_timerEx( iicMaster_TaskID, IIC_TEST_EVT, 202);
}


/*********************************************************************
 * @fn      SimpleBLEPeripheral_ProcessEvent
 *
 * @brief   Peripheral Test Task event processor.  This function
 *          is called to process USR_TEST_TIMER1_EVT events for the task. 
 * @param   task_id  - The OSAL assigned task ID.
 * @param   events - events to process adc sample function.
 *
 * @return  events not processed
 */
uint16 HalPeripheral_ProcessEvent( uint8 task_id, uint16 events )
{
	VOID task_id; // OSAL required parameter that isn't used in this function
	if(events & IIC_TEST_EVT){
        //static uint8_t testNum = 0;
		sample_i2c_master_test();
        //if(++testNum < 100)
		{
            VOID osal_start_timerEx( iicMaster_TaskID, IIC_TEST_EVT, 202);
        }
		return ( events ^ IIC_TEST_EVT );
    }  
	// Discard unknown events
    return 0;
}


